Message ordering in a messaging system

ABSTRACT

A method and system ( 200 ) for message handling in a messaging system by: receiving messages from applications associated with transactions and placing the received messages in a queue ( 230 ) for subsequent retrieval; receiving requests of prepare, commit and abort for the transactions; deferring retrieval of messages from the queue so as to occur in the same sequence as the prepare requests of the transactions with which the respective messages are associated; and retrieving from the queue messages associated with a transaction in the order in which the messages were put by the application. Preferably deferral is achieved by deferring completion of transaction commits as required to occur in the same order as the prepare message requests. Deferral may be implemented by (i) using a three phase commit protocol having Prepare, Commit, and Release phases and by implementing the Release phase within the Commit phase of the same or a different transaction so that a standard two-phase coordinator may be used, or (ii) by completing a first part of a commit phase as requested and deferring a second part of the commit phase as required. This permits appropriate message ordering in a transactional system, and in particular permits the correct implementation of a database replication system ( 220 ) using coordinated message transport that is fed the changes during database transactions.

FIELD OF THE INVENTION

[0001] This invention relates to messaging systems, and particularly to messaging systems where message ordering must be related to transactional boundaries. Such a messaging system, for example, may be used to capture database changes as they occur within a transaction.

BACKGROUND OF THE INVENTION

[0002] In the field of this invention it is known that there are various database replication systems that operate over messaging systems.

[0003] However, many of these operate by a separate phase where the database log is scraped to extract relevant data after transactions are complete. This approach has various disadvantages:

[0004] It has increased latency

[0005] It requires a more complete log record to be kept than needed for most database processing, which slows down database log writing

[0006] The scraping phase must recreate database transaction boundaries and ordering from information in the log, which is time consuming

[0007] Other systems use ‘during transaction’ capture, but have small windows of error where database changes may be recorded in the wrong order. This results in errors in the replica.

[0008] A need therefore exists for a scheme for message handling in a messaging system wherein the abovementioned disadvantage(s) may be alleviated.

STATEMENT OF THE INVENTION

[0009] In accordance with a first aspect of the present invention there is provided a method for message handling in a messaging system as claimed in claim 1.

[0010] In accordance with a second aspect of the present invention there is provided a message handling system as claimed in claim 7.

BRIEF DESCRIPTION OF THE DRAWINGS

[0011] Two schemes for message handling in a messaging system incorporating the present invention will now be described, by way of example only, with reference to the accompanying drawings, in which:

[0012]FIG. 1 shows a block-schematic diagram illustrating the well-known Two-Phase Commit protocol;

[0013]FIG. 2 shows a block-schematic diagram illustrating a messaging system having a transaction manager and two resource managers in which the present invention may be used; and

[0014]FIG. 3 shows a table depicting the sequence of steps involved in a first preferred embodiment of the invention.

DESCRIPTION OF PREFERRED EMBODIMENTS

[0015] As shown in FIG. 1, the well-known Two-Phase Commit protocol is a simple and elegant atomic commit protocol (ACP) with two types of processes: a single coordinator that decides whether to reach a global commit or abort decision, and the participants that execute the transaction's resource accesses and vote whether to commit or abort. The commit decision is made according to the following ‘global commit rule’:

[0016] if even one participant votes to abort the transaction, the coordinator has to reach a global abort decision; and

[0017] if all participants vote to commit the transaction, the coordinator will generally reach a global commit decision.

[0018] Operation of the Two-Phase Commit protocol is illustrated (for simplicity, without consideration of failures or recovery) in FIG. 1. As depicted in this figure, circles represent states, solid lines represent state changes, dashed lines represent requests/messages (labeled with the request/message types), and rectangles represent logging actions.

[0019] As shown in FIG. 1, Two-Phase Commit has two phases 110 and 120.

[0020] In the first phase (110), a coordinator process is started (usually at the site where the transaction is initialized), writes a begin commit record in its log, sends a ‘prepare’ request to the participants, and enters the wait state.

[0021] When a participant receives a ‘prepare’ request, it checks if it can commit the transaction. If it can, the participant writes a ready record in its log, sends a ‘vote_commit’ response to the coordinator, and enters the ready state. Otherwise, the participant decides to unilaterally abort the transaction (it writes an abort record in the log and sends a ‘vote_abort’ message to the coordinator; it then enters the abort state and can forget about the transaction).

[0022] In the second phase (120), after the coordinator has received votes from all participants it decides whether to commit or abort according to the global commit rule, and writes this decision in the log. If the decision is to commit, it sends a ‘global_commit’ message to all sites. Otherwise, it sends a ‘global_abort’ message to all sites that voted to commit. Finally, it writes an end of transaction record in its log. The participants finish the transaction according to the decision and write the result in their logs.

[0023] When a queuing system is used to capture changes within another resource manager such as a database, in particular where the changes are captured as part of the transaction generating the change, database changes must be captured in the correct order; and the change order must be represented in the messages on the system queue. In such a system, although it is not essential, it is highly desirable, that the change order is represented directly by the order of messages on the queue (it is essential that either (1) the change order is represented by the order of messages on the queue, or (2) that the messages are change-order-stamped in some way with the change order. Database locking will ensure that change order is the same as, or equivalent to, prepare order. However, (2) is undesirable, as the change order is not known when the messages are PUT (that is, when they are sent by placing them in an outbound queue). The messages would need to be updated as part of the PREPARE phase. Also, even though the change-order-stamp would permit the message reader to reconstruct the correct order, the message reader would not know how long it might be necessary to wait for ‘out of order’ messages). An example of such a system is a database application using user defined fields (UDFs) and triggers to generate application specific change messages into a queueing system.

[0024] Referring now to FIG. 2, such a system 200 consists of a transaction manager (TM) or resource coordinator 210, and at least two resource managers 220 and 230. One resource manager (such as a database manager, DB) 220 will be having changes applied by the application transactions. The other resource manager (for example a message queueing system, MQ) 230 will be used to capture the changes. It will be understood that in practice only a filtered set of changes is captured, and that this may be under application control or under control of an associated sub-system (such as a change exit replication system). The transaction manager (TM) 210 uses standard two phase commit protocols (prepare and commit/abort as described above in relation to FIG. 1) to coordinate the resource managers (DB and MQ) 220 and 230. Typically, many transactions will be running on different threads, and the transactions may be using multiple processors with various levels of coupling.

[0025] It will be understood that in practice the transaction manager (TM) 210 may actually be part of either the resource manager (DB) 220 or the resource manager 230 (MQ). In some environments, the order in which the resource manager (DB) 220 or the resource manager (MQ) 230 are called for prepare and for commit is well determined. For example, in an IMS environment (IMS is a well-known “Information Management System” database of IBM Corporation) both the transaction manager and 210 and database manager 230 are provided by IMS, DB 220 will be called for commit before the other resource manager MQ 230. In a CICS transaction processing system environment (CICS is a well-known transaction processing system from IBM Corporation which includes transaction coordination), the components TM 210 and DB 220 are provided by the CICS system, and resource manager MQ 230 will be called for commit before DB 220. In other environments such as XA (which is the international standard for the interface between two phase commit transaction coordinators and resource managers) with external coordinator, or DTC (the known two-phase commit coordinator of Microsoft Corporation—now integrated as part of Microsoft's operating system and “.NET” infrastructure) the order of the commit call to MQ and DB is non-deterministic, and the calls may be in parallel. CICS and IMS are trademarks of IBM Corporation.

[0026] Database locking and semantics will assure appropriate synchronization within the database; these locks will be held from when made during application processing (before the prepare phase) through until DB commit.

[0027] It is highly undesirable to change the protocols or operation of either TM or DB. The present invention does not require such a change.

[0028] The present invention is based on implementation of prepare ordering in a messaging system.

[0029] In a system where queuing is used to capture changes during a transaction, and these messages are to be replayed to recreate part or all of the changed system, a first preferred embodiment of the present invention implements unit-of-work (UOW) prepare order message ordering on queued messages by deferring execution of commit requests until commit (or abort) requests for all UOWs with earlier prepare order are complete.

[0030] In order to implement prepare ordering in this way in a system employing a transaction manager (TM) and resource managers (DB and MQ) such as described above in relation to FIG. 2, the first preferred embodiment of the present invention implements message ordering based on:

[0031] a) the time at which MQ is called for prepare on the message's UOW, and

[0032] b) within UOW, the time at which the message is put.

[0033] In this way, committed messages go through the following states:

[0034] (a) PUT

[0035] (b) PREPARED

[0036] (c) COMMITTED

[0037] (d) RELEASED

[0038] by the following system steps:

[0039] A) The message is in the PUT state immediately it is put.

[0040] B) When the PREPARE call is issued to MQ, messages within the UOW are put in the PREPARED state, and assigned a position in a global ordering for the queue (requires short queue lock).

[0041] C) When the COMMIT all instruction is issued to MQ, the messages are put into COMMITTED state.

[0042] D) When a COMMITTED message reaches the head of the unreleased part of the globally ordered queue, it is put into RELEASE state.

[0043] Messages from a unit of work may be moved from COMMITTED to RELEASED state (as in unit-of-work UOWB in step 340 of the table of FIG. 3 described in more detail below):

[0044] either D type 1) by a COMMIT of its own unit of work (UOWB),

[0045] or D type 2) by the COMMIT of an ‘earlier’ unit of work (UOWA say), where UOWA is prepared before UOWB, but committed after UOWB.

[0046] The table of FIG. 3 shows the sequence of example steps 310-390 (and corresponding system steps A-D described immediately above) for implementing such a scheme, in respect of two units of work (UOWA and UWOB) where message A is put in UOWA and message B is put in UOWB.

[0047] It will be understood that this implementation of COMMIT for a UOW is in two parts (the first part applying to messages within a particular UOW, and the second possibly also applying to messages in other UOWs as well) as follows:

[0048] part 1) for all messages in UOW, perform step (C) (COMMIT)

[0049] part 2) starting at earliest message in queue with state not RELEASED, move through messages in order while state is COMMITTED perform step (D) (RELEASE).

[0050] It will be understood that with a standard two-phase system as discussed above in relation to FIG. 1, the READ operation at example step 360 would incorrectly read message B.

[0051] Thus it will be appreciated that the procedure described above in accordance with the first preferred embodiment of the present invention is effectively three-phase commit (Prepare, Commit, Release). However, it is achieved using a standard two-phase TM protocol, as the final phase (Release) may be performed at Commit time. It may be that Release for one transaction is triggered by Commit of another.

[0052] It will be appreciated that implementation of ordering based on the order with which MQ commit was called can give wrong answers. Where DB commit is called before MQ commit, MQ commit calls may be arbitrarily deferred by TM. In that case, orderings of DB prepare, DB commit and MQ prepare will be related (not necessarily identical), but with no assured relationship to MQ commit ordering.

[0053] There may be cases where the prepare phases (and commit phases) of many UOWs overlap. Assuming correct DB locking, this can only occur where the UOW ordering is non-important.

[0054] It will be understood that the scheme of the first preferred embodiment of the present invention described above assures correct ordering and thus correct end results.

[0055] Thus it will be appreciated that in its first preferred embodiment the present invention employs a three-phase commit sequence (in which the ‘standard’ commit phase is split into two parts, commit and release) as follows:

[0056] 1) The prepare phase (first phase) can be conventional.

[0057] 2) The first part of the commit (second phase) is the part at which it is known that the message will be made available on the queue. It occurs as soon as commit is requested by the coordinator.

[0058] 3) The second part of the commit (third phase overall) releases the committed messages from the transaction to be read by a viewer of the queue. It occurs when all transactions with a prepare phase BEFORE the prepare phase for the given transaction have completed their release (or abort) phase.

[0059] Thus release phase ordering for two transactions is the same as prepare phase ordering. If we have two transactions (1 and 2), we may see the following ordering of events:

[0060] [P1, P2, C2, C1, R1, R2] or preferably

[0061] [P1, P2, C2, CR1, R2]

[0062] (Px representing Prepare of transaction x, Cx representing Commit of transaction x, Rx representing release of transaction, and CRx representing a combination of commit and release of transaction x into a single phase).

[0063] Thus, in summary, it will be understood that in its first preferred embodiment the present invention provides, in a system where queuing is used to capture changes during a transaction, and these messages are to be replayed to recreate part or all of the changed system:

[0064] a) implementation of prepare order message ordering on queued messages

[0065] b) use of a three phase commit protocol: Prepare, Commit, Release

[0066] c) implementation of Release within the Commit phase (maybe of the same UOW or another UOW as appropriate) so that a standard two-phase coordinator may be used.

[0067] It will be appreciated that the first two phases (PREPARE and COMMIT) of a three phase transaction must be persisted (probably in the transaction log) to ensure correct recovery behaviour. However, the third phase (RELEASE) does not need to be persisted. The necessary information can be reconstituted by the resource manager on restart. Thus the three-phase commit protocol does not add additional I/O to the standard two-phase protocol, and is thus likely to be only a minimal performance impact.

[0068] In a second preferred embodiment the present invention employs a two-phase commit sequence. To ensure message ordering is correctly related to database changes, where commit requests come in a different order from prepare requests, execution of early commit requests for transactions prepared later are deferred until the commit (abort) requests for the transactions prepared later have been completed. Thus, for two transactions (1 and 2), we may see the following ordering of events:

[0069] [eg P1, P2, <C2 . . . , <C1>, . . . C2>]

[0070] (<Cx . . . representing the request for transaction x to commit, . . . Cx> representing the implementation of the commit, and <Cx> representing the case where the commit phase does not need to be deferred and is implemented as soon as requested).

[0071] Thus, in summary, it will be understood that in its second preferred embodiment the present invention provides, in a system where message queuing is used to capture changes during a transaction, and these messages are to be replayed to recreate part or all of the changed system:

[0072] a) implementation of UOW prepare order message ordering on queued messages;

[0073] b) implement this by deferring execution of commit requests until commit (or abort) requests for all UOWs with earlier prepare order are complete.

[0074] Thus, it will be appreciated that the first and second preferred embodiments of the present provide alternative solutions to the same problems as discussed above, and that whereas the first preferred embodiment has the advantage that queue manager commit operations are not unnecessarily held up (but depending on coordinator implementation this could also hold up database commits for the same transaction and reduce concurrency), the second preferred embodiment has the advantage that it is not necessary to add the ‘committed but unreleased’ phase for messages.

[0075] Thus it will be understood that the essential difference between the first and second preferred embodiments of the present invention is whether Commit phase is:

[0076] a) split into Commit and Release phases, with Commit implemented on request and Release implemented as soon as validly possible, or

[0077] b) kept as a single phase deferred as necessary after request, but implemented as soon as validly possible.

[0078] Clearly, (a) is preferable for performance, but more complex.

[0079] In a typical case, prepare ordering and commit ordering for transactions will be the same. In this case the first implementation will implement the release phase immediately following the commit phase [e.g., P1, P2, C1, R1, C2, R2] (or preferably will combine the two into a single commit/release phase [e.g., P1, P2, CR1, CR2]).

[0080] In this case the second preferred embodiment of the present invention will not need to defer commit, for example:

[0081] [P1, P2, <C1>, <C2>].

[0082] It will be appreciated that the methods described above for prepare ordering in a messaging system will typically be carried out in software running on a processor (not shown), and that the software may be provided as a computer program element carried on any suitable data carrier (also not shown) such as a magnetic or optical computer disc.

[0083] It will be understood that the implementations of prepare ordering in a messaging system described above provide the advantage of permitting appropriate message ordering in a transactional system, and in particular permits the correct implementation of a database replication system using coordinated message transport that is fed the changes during database transactions. 

What is claimed is:
 1. A method for message handling in a messaging system, comprising: receiving, from application programs, messages associated with transactions and placing the received messages in a repository for subsequent retrieval; receiving requests of prepare, commit and abort for the transactions; deferring retrieval of messages from the repository so as to occur in the same sequence as the prepare requests of the transactions with which the respective messages are associated; and retrieving from the repository messages associated with a transaction in the order in which the messages were sent by an application program.
 2. The method of claim 1 wherein the step of deferring retrieval of messages comprises deferring completion of transaction commits as required to occur in the same order as the prepare requests.
 3. The method of claim 1 wherein the method comprises using a three phase commit protocol having Prepare, Commit, and Release phases; and the Release phase is implemented within the Commit phase of the same or a different transaction.
 4. The method of claim 1 wherein a Commit phase comprises first and second parts, and the step of deferring message retrieval comprises completing a first part of the Commit phase as requested and deferring a second part of the Commit phase as required.
 5. The method of claim 1 wherein the system is arranged to capture database changes as they occur within a transaction.
 6. The method of claim 5 wherein the system comprises: a transaction manager; a first resource manager comprising a database; and a second resource manager comprising a messaging manager.
 7. A message handling system comprising: means for receiving, from an application program, messages associated with transactions and placing the received messages in a repository for subsequent retrieval; means for receiving requests to prepare and commit or abort for the transactions; means for deferring retrieval of messages from the repository so as to occur in the same sequence as the prepare requests of the transactions with which the respective messages are associated; and means for retrieving from the repository messages associated with a transaction in the order in which the messages were sent by an application program.
 8. The messaging handling system of claim 7 wherein the means for deferring retrieval of messages comprises means for deferring completion of transaction commits as required to occur in the same order as the prepare requests.
 9. The messaging handling system of claim 7 wherein the system uses a three phase commit protocol having Prepare, Commit, and Release phases; and the Release phase is implemented within the Commit phase of the same or a different transaction so that a standard two-phase coordinator may be used.
 10. The messaging handling system of claim 7 wherein a Commit phase comprises first and second parts, and the means for deferring retrieval of messages comprises means for completing a first part of the Commit message as requested and deferring a second part of the Commit phase as required.
 11. The messaging handling system of claim 7 wherein the system is arranged to capture database changes as they occur within a transaction.
 12. The messaging handling system of claim 11 wherein the system comprises: a transaction manager; a first resource manager comprising a database; and a second resource manager comprising a messaging manager.
 13. A computer program product comprising computer program code recorded on a machine-readable medium, for controlling a data processing apparatus on which the program code executes to perform the method of claim
 1. 